package com.mz.ozy.activity.assets;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.TCoinsRecordAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.TbRecrodBean;
import com.mz.ozy.mvp.presenter.TbRecordPresenter;
import com.mz.ozy.mvp.view.TbRecordContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TbRecordActivity extends BaseActivity<TbRecordContract.View, TbRecordPresenter> implements TbRecordContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private TCoinsRecordAdapter mAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<TbRecrodBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcoins_record);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText(R.string.tb_record);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TCoinsRecordAdapter(this, mBeanList);
        mRecyclerView.setAdapter(mAdapter);
        mSmartRefreshLayout.setEnableAutoLoadmore(true);
        mSmartRefreshLayout.autoRefresh();
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getTbRecord(TbRecordActivity.this, String.valueOf(pageNo),"",1);


            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getTbRecord(TbRecordActivity.this, String.valueOf(pageNo),"",1);


            }
        });
    }

    @Override
    public void getTbRecordSuccess(TbRecrodBean bean) {
        if (tag == 0) {
            mSmartRefreshLayout.setLoadmoreFinished(false);
            mSmartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mSmartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

}
